{% extends "base.html" %}
{% block head %}
{{ super() }}
<link rel="stylesheet" href="{{ url_for('static', filename='css/rules.css') }}">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $('#formSelect').change(function(){
        if ($(this).val() == 'not_terminal') {
            $('#statements').show();
            $('#statements_term').hide();
        } else if ($(this).val() == 'terminal') {
            $('#statements').hide();
            $('#statements_term').show();
        } else {
            $('#statements').hide();
            $('#statements_term').hide();
        }
    });
    // Añadir listeners de eventos para los cambios en los selects go_to y jump
    $('#statements_term-go_to').change(function(){
        $('#go_to').val($(this).val());
    });
    $('#statements_term-jump').change(function(){
        $('#jump').val($(this).val());
    });
});

    // Función para actualizar el valor del input oculto con la table.family seleccionada
function updateHiddenInput() {
    var selectedTable = document.getElementById('chain').value;
    var hiddenInput = document.getElementById('family');
    hiddenInput.value = selectedTable.split('-')[1].trim(); // Obtener la parte de la familia de la tabla seleccionada
}

</script>
{% endblock %}
{% block content %}
<div class="container">
    <h1>Create Rule</h1>
    <form method="POST" action="/rules/create_rule" class="needs-validation" novalidate>
        {{ form.csrf_token}}
        <div class="form-group">
            {{ form.description.label(class="form-control-label") }}
            {{ form.description(class="form-control form-control-lg") }}
        </div>
        <div class="form-group">
            {{ form.chain.label }}
            <select id="chain" name="chain" class="form-control" onchange="updateHiddenInput()">
                {% for chain in chains %}
                <option value="{{ chain.id }}&&{{chain.table.family}}&&{{chain.table.name}}&&{{chain.name}}">Name: {{ chain.name }} - Family: {{ chain.family }} - Table: {{ chain.table.name }}</option>
                {% endfor %}
            </select>
            {% if form.chain.errors %}
            <ul class="errors">
                {% for error in form.chain.errors %}
                <li>{{ error }}</li>
                {% endfor %}
            </ul>
            {% endif %}
        </div>

        <div class="form-group">
            <label for="formSelect">Selects the statements:</label>
            <select class="form-control" id="formSelect" name="statement_select">
                <option value="">--Selects--</option>
                <option value="not_terminal">Not terminal statements</option>
                <option value="terminal">Terminal statements</option>
            </select>
        </div>
        {% if form.chain.errors == [] and form.description.errors == [] %}
        <div class="alert alert-danger" role="alert">
            <strong>Warning!</strong> The rule could not be created because there are errors in the statement form.
        </div>
        {% endif %}
        <div id="statements" class="form-group" style="display:none">
            {% for field in form.statements %}
                {% if field.name != 'csrf_token' and field.type != 'SubmitField' %}
                    {% if field.name == 'statements-nflog' %}
                    <div class="form-group">
                        {{ field.label(class="form-control-label") }}
                        <p>Insert the name of the flog flow</p>
                        {{ field(class="form-control form-control-lg") }}
                        {% if field.errors %}
                        <ul class="errors">
                            {% for error in field.errors %}
                            <li>{{ error }}</li>
                            {% endfor %}
                        </ul>
                        {% endif %}
                    </div>
                    {% elif field.name == 'statements-src_ip_objects' %}
                    <div class="form-group">
                        {{ field.label(class="form-control-label") }}
                        <p>Insert the name of the source object</p>
                        <select id="statements-src_ip_objects" name="statements-src_ip_objects" class="form-control">
                            <option>--Selects--</option>
                            {% for object in objects %}
                            <option value="{{ object.name }}">{{ object.name }}</option>
                            {% endfor %}
                        </select>
                        <input type="hidden" id="src_ip_objects" name="src_ip_objects" value=""></input>
                        {% if field.errors %}
                        <ul class="errors">
                            {% for error in field.errors %}
                            <li>{{ error }}</li>
                            {% endfor %}
                        </ul>
                        {% endif %}
                    </div>
                    {% elif field.name == 'statements-dst_ip_objects' %}
                    <div class="form-group">
                        {{ field.label(class="form-control-label") }}
                        <p>Insert the name of the destination object</p>
                        <select id="statements-dst_ip_objects" name="statements-dst_ip_objects" class="form-control">
                            <option>--Selects--</option>
                            {% for object in objects %}
                            <option value="{{ object.name }}">{{ object.name }}</option>
                            {% endfor %}
                        </select>
                        <input type="hidden" id="dst_ip_objects" name="dst_ip_objects" value=""></input>
                        {% if field.errors %}
                        <ul class="errors">
                            {% for error in field.errors %}
                            <li>{{ error }}</li>
                            {% endfor %}
                        </ul>
                        {% endif %}
                    </div>
                    {% elif field.name == 'statements-src_port_objects' %}
                    <div class="form-group">
                        {{ field.label(class="form-control-label") }}
                        <p>Insert the name of the source port object</p>
                        <select id="statements-src_port_objects" name="statements-src_port_objects" class="form-control">
                            <option>--Selects--</option>
                            {% for object in objects %}
                            <option value="{{ object.name }}">{{ object.name }}</option>
                            {% endfor %}
                        </select>
                        <input type="hidden" id="src_port_objects" name="src_port_objects" value=""></input>
                        {% if field.errors %}
                        <ul class="errors">
                            {% for error in field.errors %}
                            <li>{{ error }}</li>
                            {% endfor %}
                        </ul>
                        {% endif %}
                    </div>
                    {% elif field.name == 'statements-dst_port_objects' %}
                    <div class="form-group">
                        {{ field.label(class="form-control-label") }}
                        <p>Insert the name of the destination port object</p>
                        <select id="statements-dst_port_objects" name="statements-dst_port_objects" class="form-control">
                            <option>--Selects--</option>
                            {% for object in objects %}
                            <option value="{{ object.name }}">{{ object.name }}</option>
                            {% endfor %}
                        </select>
                        <input type="hidden" id="dst_port_objects" name="dst_port_objects" value=""></input>
                        {% if field.errors %}
                        <ul class="errors">
                            {% for error in field.errors %}
                            <li>{{ error }}</li>
                            {% endfor %}
                        </ul>
                        {% endif %}
                    </div>
                    {% else %}
                    <div class="form-group">
                        {{ field.label(class="form-control-label") }}
                        {{ field(class="form-control form-control-lg") }}
                        {% if field.errors %}
                        <ul class="errors">
                            {% for error in field.errors %}
                            <li>{{ error }}</li>
                            {% endfor %}
                        </ul>
                        {% endif %}
                    </div>
                {% endif %}
                {% endif %}
            {% endfor %}
        </div>
        <div id="statements_term" class="form-group" style="display:none">
            {% for field in form.statements_term %}
                {% if field.name != 'csrf_token' and field.type != 'SubmitField' %}

                    {% if field.name == 'statements_term-jump' %}
                    <div class="form-group">
                        {{ field.label(class="form-control-label") }}
                        <p>Insert the name of the chain to jump</p>
                        <select id="statements_term-jump" name="statements_term-jump" class="form-control">
                            <option>--Selects--</option>
                            {% for chain in chains %}
                            
                            <option value="{{ chain.name }}">{{ chain.name }}</option>
                            {% endfor %}
                            </select>
                            <input type="hidden" id="jump" name="jump" value=""></input>

                        {% if field.errors %}
                        <ul class="errors">
                            {% for error in field.errors %}
                            <li>{{ error }}</li>
                            {% endfor %}
                        </ul>
                        {% endif %}
                    </div>
                    {% elif field.name == 'statements_term-go_to' %}
                    <div class="form-group">
                        {{ field.label(class="form-control-label") }}
                        <p>Insert the name of the chain to go to</p>
                        <select id="statements_term-go_to" name="statements_term-go_to" class="form-control">
                            <option>--Selects--</option>
                            {% for chain in chains %}
                            <option value="{{ chain.name }}">{{ chain.name }}</option>
                            {% endfor %}
                            </select>
                            <input type="hidden" id="go_to" name="go_to" value=""></input>

                        {% if field.errors %}
                        <ul class="errors">
                            {% for error in field.errors %}
                            <li>{{ error }}</li>
                            {% endfor %}
                        </ul>
                        {% endif %}
                    </div>
                    {% elif field.name == 'statements_term-src_ip_objects' %}
                    <div class="form-group">
                        {{ field.label(class="form-control-label") }}
                        <p>Insert the name of the source object</p>
                        <select id="statements_term-src_ip_objects" name="statements_term-src_ip_objects" class="form-control">
                            <option>--Selects--</option>
                            {% for object in objects %}
                            <option value="{{ object.name }}">{{ object.name }}</option>
                            {% endfor %}
                        </select>
                        <input type="hidden" id="src_ip_objects" name="src_ip_objects" value=""></input>
                        {% if field.errors %}
                        <ul class="errors">
                            {% for error in field.errors %}
                            <li>{{ error }}</li>
                            {% endfor %}
                        </ul>
                        {% endif %}
                    </div>
                    {% elif field.name == 'statements_term-dst_ip_objects' %}
                    <div class="form-group">
                        {{ field.label(class="form-control-label") }}
                        <p>Insert the name of the destination object</p>
                        <select id="statements_term-dst_ip_objects" name="statements_term-dst_ip_objects" class="form-control">
                            <option>--Selects--</option>
                            {% for object in objects %}
                            <option value="{{ object.name }}">{{ object.name }}</option>
                            {% endfor %}
                        </select>
                        <input type="hidden" id="dst_ip_objects" name="dst_ip_objects" value=""></input>
                        {% if field.errors %}
                        <ul class="errors">
                            {% for error in field.errors %}
                            <li>{{ error }}</li>
                            {% endfor %}
                        </ul>
                        {% endif %}
                    </div>
                    {% elif field.name == 'statements_term-src_port_objects' %}
                    <div class="form-group">
                        {{ field.label(class="form-control-label") }}
                        <p>Insert the name of the source port object</p>
                        <select id="statements_term-src_port_objects" name="statements_term-src_port_objects" class="form-control">
                            <option>--Selects--</option>
                            {% for object in objects %}
                            <option value="{{ object.name }}">{{ object.name }}</option>
                            {% endfor %}
                        </select>
                        <input type="hidden" id="src_port_objects" name="src_port_objects" value=""></input>
                        {% if field.errors %}
                        <ul class="errors">
                            {% for error in field.errors %}
                            <li>{{ error }}</li>
                            {% endfor %}
                        </ul>
                        {% endif %}
                    </div>
                    {% elif field.name == 'statements_term-dst_port_objects' %}
                    <div class="form-group">
                        {{ field.label(class="form-control-label") }}
                        <p>Insert the name of the destination port object</p>
                        <select id="statements_term-dst_port_objects" name="statements_term-dst_port_objects" class="form-control">
                            <option>--Selects--</option>
                            {% for object in objects %}
                            <option value="{{ object.name }}">{{ object.name }}</option>
                            {% endfor %}
                        </select>
                        <input type="hidden" id="dst_port_objects" name="dst_port_objects" value=""></input>
                        {% if field.errors %}
                        <ul class="errors">
                            {% for error in field.errors %}
                            <li>{{ error }}</li>
                            {% endfor %}
                        </ul>
                        {% endif %}
                    </div>

                    {% else %}                        
                    <div class="form-group">
                        {{ field.label(class="form-control-label") }}
                        {{ field(class="form-control form-control-lg") }}
                        {% if field.errors %}
                        <ul class="errors">
                            {% for error in field.errors %}
                            <li>{{ error }}</li>
                            {% endfor %}
                        </ul>
                        {% endif %}
                    </div>
                    {% endif %}
                {% endif %}
            {% endfor %}
        </div>
        <input type="hidden" id="family" name="family" value="">
        {% if form.errors %}
        <ul class="errors">
            {% for error in form.errors %}
            <li>{{ error }}</li>
            {% endfor %}
        </ul>
        {% endif %}
        <div class="form-group">
            {{ form.submit (class="btn btn-primary") }}
        </div>
        {{ msg }}
    </form>
</div>
{% endblock %}